<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>React生命周期(新)</title>
  </head>

  <body>
    <!-- 准备好一个区域 -->
    <div id="test"></div>

    <script
      type="text/javascript"
      src="../js/17.0.1/react.development.js"
    ></script>
    <script
      type="text/javascript"
      src="../js/17.0.1/react-dom.development.js"
    ></script>
    <script type="text/javascript" src="../js/17.0.1/babel.min.js"></script>
    <script type="text/javascript" src="../js/17.0.1/prop-types.js"></script>
    <script type="text/babel">
      // 创建组件
      class News extends React.Component {
        state = { newsArr: [] };

        // 挂载完毕
        componentDidMount() {
          setInterval(() => {
            const { newsArr } = this.state;
            const news = "新闻" + (newsArr.length + 1);
            this.setState({ newsArr: [news, ...newsArr] });
          }, 1000);
        }

        render() {
          return (
            <div>
              <div className="list">
                {this.state.newsArr.map((n, i) => {
                  return <div key={i}>{n}</div>;
                })}
              </div>
            </div>
          );
        }
      }

      ReactDOM.render(<News />, document.getElementById("test"));
    </script>
  </body>
</html>
